package com.xb.scheduled;

import com.xb.entity.Meeting;
import com.xb.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class MeetingScheduled {

    @Autowired
    MeetingService meetingService;

    /**
     * 每年的每月的每日的每小时的每分钟的第一秒执行一次
     */
    @Scheduled(cron = "* * * * * ?")
    public void meetingJob(){

        List<Meeting> meetingList = meetingService.findByStatusNot(2L);

        if (null != meetingList){
            for (Meeting meeting : meetingList) {

                Date startTime = meeting.getStartTime();
                Date endTime = meeting.getEndTime();

                if (new Date().getTime() > startTime.getTime() && meeting.getStatus().longValue() == 0L){
                    // 会议开始
                    meetingService.updateMeetingStatus(meeting.getId(),1L);
                }
                if (new Date().getTime() > endTime.getTime() && meeting.getStatus().longValue() == 1L){
                    // 会议结束
                    meetingService.updateMeetingStatus(meeting.getId(),2L);
                }
            }
        }

    }
}
